package com.hanyxx.annotaion;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author 菠菜饭团
 * @description: 测试框架演示
 * @date 2020/12/26
 */
public class CalculatorTest {
    public static void main(String[] args) throws IOException {
        int number = 0 ;
        BufferedWriter bw = new BufferedWriter(new FileWriter("bug.txt"));
        Method[] methods = Calculator.class.getMethods();
        for (Method method : methods) {
            //判断方法是否有注解checkException
            if(method.isAnnotationPresent(check.class)){
                try {
                    method.invoke(Calculator.class.newInstance());
                } catch (Exception e) {
                    number ++;
                    bw.write("----------------------------");
                    bw.newLine();
                    bw.write("运行"+method.getName()+"方法时，捕获异常："+e.getCause());
                    bw.newLine();
                    bw.flush();
                }
            }
        }
        bw.write("----------------------------");
        bw.newLine();
        bw.write("本次运行共捕获 "+number+" 个异常");
        bw.flush();
        bw.close();
    }
}
